<?php

class HomeController extends Controller {
	
 
	public function accessRules() {
		return array(
			array('allow','actions'=>array('index'),'users'=>array('*')),
			array('deny','expression'=>'$user->isGuest'),
		
		);
	}
	
	public function actionIndex() {
		if (! Yii::app ()->user->isGuest){
			$this->forward ( 'statistic' );
			
		}
		
		$this->layout = 'application.views.layouts.home';
		$this->render ( 'index' );
	}
	public function actionIndex2() {
		$this->layout = 'application.views.layouts.main';
		$this->render ( 'index2' );
	}
	
	public function actionStatistic() {
		if (Yii::app ()->user->me->domain  === null) {
			
			$this->redirect ( array ('/setting/domain' ) );
		}
		
		$criteria = new EMongoCriteria ();
		
		$criteria->addCond ( 'domain', 'eq', Yii::app ()->user->me->domain );
		$criteria->addCond ( 'date', '>=', date ( 'Ymd', strtotime ( '-1 year' ) ) );
		//		
		//		print_r($criteria->getConditions());
		$statistics = ViewStatistic::model ()->findAll ( $criteria );
		
		//		print_r($statistics);
		

		$statistics = array_reverse ( $statistics );
		
		$today = array_fill ( 0, 24, 0 );
		$index = 0;
		
		$last_month_days = date ( 't', strtotime ( '-1 months' ) );
		$monthly = array ();
		
		$yearly = array ();
		
		foreach ( $statistics as $day ) {
			
			//		echo $day->date;
			//today;
			if ($index == 0 && $day->date == date('Ymd'))
				$today = $day->hours + $today;
			
			if ($index < $last_month_days) {
				$key = substr ( $day->date, 6, 2 );
				$monthly [$key] = $day->views;
			}
			
			$key = substr ( $day->date, 0, 6 );
			if (isset ( $yearly [$key] )) {
				$yearly [$key] = $day->views + $yearly [$key];
			} else
				$yearly [$key] = $day->views;
			
			$index ++;
		}
		if (count ( $yearly ) < 13) {
			
			$key = date ( 'Ym', strtotime ( '-1 year' ) );
			
			while ( $key <= date ( 'Ym' ) ) {
				if (! isset ( $yearly [$key] )) {
					$yearly [$key] = 0;
				}
				$key = date ( 'Ym', strtotime ( '+1 months ' . $key . '01000000' ) );
			}
		}
		//
		if (count ( $monthly ) < $last_month_days) {
			
			$key = date ( 'Ymd', strtotime ( '-1 month' ) );
			
			while ( $key <= date ( 'Ymd' ) ) {
				if (! isset ( $monthly [substr ( $key, 6, 2 )] )) {
					$monthly [substr ( $key, 6, 2 )] = 0;
				}
				$key = date ( 'Ymd', strtotime ( '+1 day ' . $key . '000000' ) );
			}
		}
		
		ksort ( $yearly );
		ksort ( $monthly );
		ksort ( $today );
		//month
		$monthly = array_slice ( $monthly, date ( 'd' ), $last_month_days - date ( 'd' ), true ) + array_slice ( $monthly, 0, date ( 'd' ), true );
		
		
		//LINK 
		$criteria = new EMongoCriteria ();
		
		$criteria->addCond ( 'domain', 'eq', Yii::app ()->user->me->domain );
		$link_count = LinkStatistic::model()->count($criteria);
		
		$criteria->setLimit(10);
		$criteria->sort('count',EMongoCriteria::SORT_DESC);
		$links = LinkStatistic::model()->findAll($criteria);
		
		$criteria = new EMongoCriteria ();
		$criteria->addCond ( 'domain', 'eq', Yii::app ()->user->me->domain );
		$criteria->setLimit(10);
		$criteria->sort('count',EMongoCriteria::SORT_DESC);
		$keywords = EngineKeywordStatistic::model()->findAll($criteria);
		
		
		$data = compact ('today','monthly','yearly','links','keywords','link_count');
		$this->render ( 'statistic', array ('data' => $data) );
	}

}